package com.example.m6_activity_lifecycle

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResult
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.m6_activity_lifecycle.MainActivity.Companion.EMPTY_FIELD
import com.example.m6_activity_lifecycle.MainActivity.Companion.EXTRA_HAVE_MIDDLE_NAME
import com.example.m6_activity_lifecycle.MainActivity.Companion.EXTRA_USER_INFO
import com.example.m6_activity_lifecycle.MainActivity.Companion.LOG_NAME
import com.example.m6_activity_lifecycle.databinding.ActivityUserInfoBinding
import com.example.m6_activity_lifecycle.models.UserInfo

class UserInfoActivity : AppCompatActivity() {
    private var haveMiddleName: Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityUserInfoBinding = ActivityUserInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        haveMiddleName = intent.getBooleanExtra(EXTRA_HAVE_MIDDLE_NAME, false)
        if (!haveMiddleName) binding.middleNameTextInputLayout.visibility = View.GONE
        binding.saveButton.setOnClickListener {
            val userInfo: UserInfo = UserInfo(
                binding.firstNameEditText.text.toString(),
                binding.lastNameEditText.text.toString(),
                binding.middleNameEditText.text.toString()
            )
            if (userInfo.lastName != "" && (if (haveMiddleName) userInfo.middleName != "" else true)
                && userInfo.firstName != ""
            ) {
                val data: Intent = Intent()
                data.putExtra(EXTRA_USER_INFO, userInfo)
                setResult(Activity.RESULT_OK, data)
                finish()
            } else
                Toast.makeText(this, EMPTY_FIELD, Toast.LENGTH_LONG).show()
        }
        Log.d(LOG_NAME, "UserInfoActivity created")
    }

    override fun onStart() {
        super.onStart()
        Log.d(LOG_NAME, "UserInfoActivity started")
    }

    override fun onResume() {
        super.onResume()
        Log.d(LOG_NAME, "UserInfoActivity resumed")
    }

    override fun onPause() {
        super.onPause()
        Log.d(LOG_NAME, "UserInfoActivity paused")
    }

    override fun onStop() {
        super.onStop()
        Log.d(LOG_NAME, "UserInfoActivity stopped")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(LOG_NAME, "UserInfoActivity destroyed")
    }
}